
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_errno.h" //==IOT_SUCCESS =0 

#include "mpu_i2c.h" 
#include "hi_io.h"   //上拉、复用
#include "hi_gpio.h" //hi_gpio_set_dir()、hi_gpio_set(get)_output(input)_val()
#include "hi_time.h"



/**
 * 
 * 
 * Soft iic code 2021.09.04 By HelloKun
 * 
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_errno.h" //==IOT_SUCCESS =0 

#include "mpu_i2c.h" 
#include "hi_io.h"   //上拉、复用
#include "hi_gpio.h" //hi_gpio_set_dir()、hi_gpio_set(get)_output(input)_val()
#include "hi_time.h"


//IIC SCL:P10  SDA:P11
void MPU_IIC_Init(void)
{					     

	MPU_IIC_SCL_OUT_1();
	MPU_IIC_SDA_OUT_1();

}


void MPU_IIC_Start(void)
{
	MPU_SDA_OUT();     //sda     
	MPU_IIC_SDA_OUT_1();	  	  
	MPU_IIC_SCL_OUT_1();
	hi_udelay(2);
 	MPU_IIC_SDA_OUT_0();//START:when CLK is high,DATA change form high to low 
	hi_udelay(2);
	MPU_IIC_SCL_OUT_0();      
}	  

void MPU_IIC_Stop(void)
{
	MPU_SDA_OUT();//sda     
	MPU_IIC_SCL_OUT_0();
	MPU_IIC_SDA_OUT_0();//STOP:when CLK is high DATA change form low to high
 	hi_udelay(2);
	MPU_IIC_SCL_OUT_1(); 
	MPU_IIC_SDA_OUT_1();// I2C   
	hi_udelay(2);							   	
}

   
void IIC_Ack(void)
{
	MPU_IIC_SCL_OUT_0();
	MPU_SDA_OUT();    
	MPU_IIC_SDA_OUT_0(); 
	hi_udelay(2);
	MPU_IIC_SCL_OUT_1();   
	hi_udelay(2);  
	MPU_IIC_SCL_OUT_0();   
}
    	    
void IIC_NAck(void)
{
	MPU_IIC_SCL_OUT_0();
	MPU_SDA_OUT();
	MPU_IIC_SDA_OUT_1();
	hi_udelay(2);
	MPU_IIC_SCL_OUT_1();
	hi_udelay(2);
	MPU_IIC_SCL_OUT_0();
}


uint8_t MPU_IIC_Wait_Ack(void)
{
	uint8_t ucErrTime=0;
	MPU_SDA_IN();      
	MPU_IIC_SDA_OUT_1();hi_udelay(2);	  
	MPU_IIC_SCL_OUT_1();hi_udelay(2);
	uint8_t val;	 
	while(MPU_READ_SDA) 
	{
		ucErrTime++;
		if(ucErrTime>250)
		{
			MPU_IIC_Stop();
			return 1;
		}
	}
	MPU_IIC_SCL_OUT_0();
	return 0;  
} 


void MPU_IIC_Send_Byte(uint8_t txd)
{                        
    uint8_t t,val=0;   
	MPU_SDA_OUT();   
    MPU_IIC_SCL_OUT_0();
    for(t=0;t<8;t++)
    {              
        val=(txd&0x80)>>7; 
		hi_gpio_set_ouput_val(11,val);
        txd<<=1; 	  
		MPU_IIC_SCL_OUT_1();
		hi_udelay(2); 
		MPU_IIC_SCL_OUT_0();	
		hi_udelay(2);
    }	 
} 


uint8_t MPU_IIC_Read_Byte(unsigned char ack)
{
	unsigned char i,receive=0,val=0;
	MPU_SDA_IN();
    for(i=0;i<8;i++ ) 
	{
        MPU_IIC_SCL_OUT_0(); 
        hi_udelay(2);
		MPU_IIC_SCL_OUT_1();
        receive<<=1; 
        if(MPU_READ_SDA)receive++;  //     
		hi_udelay(2); 
    }					 
    if (!ack)
        IIC_NAck();//    nACK
    else
        IIC_Ack(); //    ACK   
    return receive;
}
*/









